//
//  UserMainViewController.m
//  HeadYang
//
//  Created by sunmingming on 15/7/10.
//  Copyright (c) 2015年 xiaoluoli. All rights reserved.
//

#import "UserMainViewController.h"
#import "UserUsViewController.h"
#import "UserOpinionVControllerViewController.h"
#import "UserOrdersViewController.h"
#import "UserCouponViewController.h"
#import "UserHistoryViewController.h"
#import "UserShowViewController.h"
#import "UserInfoViewController.h"
#import "UIImageView+AFNetworking.h"

@interface UserMainViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) UITableView *mainTableView;

@end

@implementation UserMainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"我的";
        self.tabBarItem.image = [UIImage imageNamed:@"tabbaritem3"];
        
        NSLog(@"height:  %f",KDeviceHeight);
        self.mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, KDeviceHeight - 64 - 49) style:UITableViewStylePlain];
        self.mainTableView.dataSource = self;
        self.mainTableView.delegate = self;
        self.mainTableView.backgroundColor = SMMBGWhite;
        self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:self.mainTableView];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
//    [self fetchHead];
}

-(void)fetchHead{
    NSDictionary *parameters=@{@"token":APPALL.myUserItem.token};
    [SVProgressHUD showWithStatus:@"loading..."];
    [AVCloud callFunctionInBackground:@"user_headsee" withParameters:parameters block:^(id object, NSError *error) {
        // 执行结果
        if (!error && object) {
            NSLog(@"avget:%@",object);
            NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
            switch ([[aSucData valueForKey:@"success"] intValue]) {
                case 0:
                {
                    [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                }
                    break;
                case 1:
                {
                    NSLog(@"yeahhead....");
                }
                    break;
                default:
                    break;
                    
            }
            [SVProgressHUD dismiss];
        } else {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        }
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 1;
        case 1:
            return 2;
        case 2:
            return 2;
        case 3:
            return 2;
        default:
            return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10.0f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
        return 100.0f;
    return 44.0f;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    switch (indexPath.section) {
        case 0:
        {
            static NSString *inde = @"UserHeadID";
            cell = [tableView dequeueReusableCellWithIdentifier:inde];
            if (cell == nil) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"UserHeadCell" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            UIImageView *iv = (UIImageView*)[cell viewWithTag:99];
            iv.layer.masksToBounds =YES;
            iv.layer.cornerRadius = iv.frame.size.width/2;
            [iv setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@user/head?token=%@",APPALL.globalUrl,APPALL.myUserItem.token]]
                              placeholderImage:[UIImage imageNamed:@"default_photo.png"]];
            UILabel *label1 = (UILabel*)[cell viewWithTag:1];
            label1.text = APPALL.myUserItem.username;
        }
            break;
        case 1:
        {
            static NSString *inde = @"normalmidid";
            cell = [tableView dequeueReusableCellWithIdentifier:inde];
            if (cell == nil) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"NormalCellMid" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            UILabel *lb = (UILabel*)[cell viewWithTag:1];
            switch (indexPath.row) {
                case 0:
                    lb.text = @"历史订单";
                    break;
                case 1:
                    lb.text = @"我的订单";
                    break;
                case 2:
                    lb.text = @"我的收藏";
                    break;
                default:
                    break;
            }
        }
            break;
        case 2:
        {
            static NSString *inde = @"normalmidid";
            cell = [tableView dequeueReusableCellWithIdentifier:inde];
            if (cell == nil) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"NormalCellMid" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            UILabel *lb = (UILabel*)[cell viewWithTag:1];
            switch (indexPath.row) {
                case 0:
                    lb.text = @"我的晒单";
                    break;
                case 1:
                    lb.text = @"我的优惠券";
                    break;
                default:
                    break;
            }
        }
            break;
        case 3:
        {
            static NSString *inde = @"normalmidid";
            cell = [tableView dequeueReusableCellWithIdentifier:inde];
            if (cell == nil) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"NormalCellMid" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            UILabel *lb = (UILabel*)[cell viewWithTag:1];
            switch (indexPath.row) {
                case 0:
                    lb.text = @"关于我们";
                    break;
                case 1:
                    lb.text = @"意见反馈";
                    break;
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:{
            UserInfoViewController *vc = [UserInfoViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:
            switch (indexPath.row) {
                case 0:{
                    UserHistoryViewController *vc = [UserHistoryViewController new];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 1:{
                    UserOrdersViewController *vc = [UserOrdersViewController new];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 2:{
                    UserInfoViewController *vc = [UserInfoViewController new];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                default:
                    break;
            }
            break;
        case 2:
            switch (indexPath.row) {
                case 0:{
                    UserShowViewController *vc = [UserShowViewController new];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 1:{
                    UserCouponViewController *vc = [UserCouponViewController new];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                default:
                    break;
            }
            break;
        case 3:
            switch (indexPath.row) {
                case 0:{
                    UserUsViewController *vc = [UserUsViewController new];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 1:{
                    UserOpinionVControllerViewController *vc = [UserOpinionVControllerViewController new];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                default:
                    break;
            }
            break;
        default:
            break;
    }
}

@end